import http from '../../apis/api.js';
import allFacilities from '../../utils/facilities.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginInfo: null,
    msgImge: '/images/msg.png',
    evaluate: '/images/evaluate.png',
    isShowta: '',/** */
    isCollect: false,/**是否收藏 */
    id: '',
    userImage: '',
    storeIntros: null,
    storeDtl: null,
    vouchers: [],/**代金券列表 */
    selectVoucher: {},
    swiperConf: {
      autoplay: true,
      interval: 2000,
      circular: true,
      current: 0,
      showCurrent: 1,
    },
    isShowFacilities: false,
    allFacilities,
    mapConfig: {
      longitude: '',/**中心经度 */
      latitude: '',/**中心纬度 */
      hasMarkers: false,
      scale: 16,
      markers: [{
        id: 0,
        latitude: '',
        longitude: '',
        title: ''
      }],
      enableZoom: false,
      enableScroll: false,
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.id = options.id;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.swiperConf.current = 0;
    self.data.swiperConf.showCurrent = 1;
    util.getUserInfo().then(res => {
      self.data.userImage = res.userInfo.avatarUrl;
      self.setData({
        userImage: self.data.userImage,
        swiperConf: self.data.swiperConf
      })
    })
   
    http.vouchersDetail({id:self.data.id}).then(res=>{
      // 格式化名宿标记
      res.data.storeLableList = res.data.storeLable.split(',');
      let facilities = res.data.facilities.split(",");
      res.data.facilitiesList = [];
      res.data.picList = [];
      res.data.storePics.forEach(el => {
        if (el.picType == "51000102") {
          res.data.picList.push(el.inPath);
        }
      })
      self.data.allFacilities.forEach(el => {
        facilities.forEach(item => {
          if (el.name.indexOf(item) > -1) {
            res.data.facilitiesList.push(el)
          }
        })
      })
      self.data.mapConfig.latitude = res.data.latitude;
      self.data.mapConfig.longitude = res.data.longitude;
      self.data.mapConfig.markers[0].latitude = res.data.latitude;
      self.data.mapConfig.markers[0].longitude = res.data.longitude;
      self.data.mapConfig.markers[0].title = res.data.address;
      self.data.storeDtl = res.data;
      //格式化名宿设施
      self.setData({
        storeDtl: self.data.storeDtl,
        mapConfig: self.data.mapConfig
      })
      http.storeIntros({//获取民宿介绍
        storeNo: res.data.storeNo,
        introType: '50800104'
      }).then(result => {
        self.data.storeIntros = result.data[0].contect.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin: 0 auto;"');
        self.setData({
          storeIntros: self.data.storeIntros
        })
      })
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      url: self.data.storeDtl.storePics[0].inPath,
      title: self.data.storeDtl.storeName,
      path: `/pages/detail/detail?storeNo=${self.data.storeNo}`
    }
  },
  gotohere(e) {
    wx.showLoading({
      title: '打开内置地图...',
    })
    wx.openLocation({//​使用微信内置地图查看位置。
      latitude: parseFloat(self.data.mapConfig.latitude),//要去的纬度-地址
      longitude: parseFloat(self.data.mapConfig.longitude),//要去的经度-地址
      name: self.data.mapConfig.markers.title,
      address: self.data.mapConfig.markers.title,
      success(res) {
        wx.hideLoading()
      },
      fail(res) {
        wx.hideLoading();
        wx.showToast({
          title: '内置地图打开失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  orderNow() {
    wx.navigateTo({
      url: '/pages/order/order',
    })
  },
  swiperChange(e) {/**切换民宿图片 */
    self.data.swiperConf.current = e.detail.current;
    self.data.swiperConf.showCurrent = e.detail.current + 1;
    self.setData({
      swiperConf: self.data.swiperConf
    })
  },
  handleTap(e) {
    let data = e.currentTarget.dataset.btntype;
    switch (data) {
      case "phone":
        wx.makePhoneCall({
          phoneNumber: self.data.storeDtl.serTel
        })
        break;
      case "evaluate":
        wx.navigateTo({
          url: `/pages/evaluate/evaluate?custNo=${self.data.storeDtl.custNo}&storeNo=${self.data.storeDtl.storeNo}&storeName=${encodeURIComponent(self.data.storeDtl.storeName)}`,
        })
        break;
      case "usecoupon":
        wx.navigateTo({
          url: `/pages/vouchersUse/vouchersUse?id=${self.data.id}`,
        })
        break;
      case "order":
      wx.navigateTo({
        url: `/pages/order/order?storeNo=${self.data.storeDtl.storeNo}&prodNo=${self.data.storeDtl.prodNo}`,
      })
        break;
      case "evaluateList":
        wx.navigateTo({
          url: `/pages/evaluateList/evaluateList?storeNo=${self.data.storeDtl.storeNo}`,
        })
        break;
        
    }
  },
  goBack() {
    wx.navigateBack()
  },
  radioChange(e) {
    let value = e.detail.value;
    self.data.vouchers.forEach(el => {
      if (el.id == value) {
        self.data.selectVoucher = el;
      }
    })
    self.setData({
      selectVoucher: self.data.selectVoucher
    })
  },
  changeShowFacilities(e) {
    self.data.isShowFacilities = !self.data.isShowFacilities;
    self.setData({
      isShowFacilities: self.data.isShowFacilities
    })
  },
  previewImage(e) {
    /**点击查看图片**/
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: index, // 当前显示图片的http链接
      urls: self.data.storeDtl.picList // 需要预览的图片http链接列表
    })
  }
})